<template>
  <div  class="wrapper">
    <div>
      <FormPanel
        ref="from"
        :list="initForm"
        :cols="3"
        :formType="value.type"
        :labelWidth="120"
        :isSubmitBtn="false"
        :collapse="false"
        @formChange="changeHandle"
      />
    </div>


    <FilterTable
      height="auto"
      ref="tableQuestion"
      :columns="questionColumns"
      :isSelectColumn="isSelectColumn"
      selectionType="single"
      :isMemoryPagination="true"
      :onSyncTableData="syncPjFun"
      :datakey="'records'"
      :isToperInfo="false"
      :onColumnsChange="questionColumns => (this.questionColumns = questionColumns)"
      columnsRef="notification_maintain_info_02">

      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addPositionFun()">添加问题</el-button>
      </template>
    </FilterTable>

    <BaseDialog
      :visible.sync="visibleQuestion"
      :title="'问题查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">

      <questionInfo v-model="searchData" @close="closeFun(1)" @selectedOrder="selectedFun" />
    </BaseDialog>

    <div class="dialogBottmView">
      <el-button @click="closeDialog('closeDialog')">关 闭</el-button>
      <el-button :loading="loadingFlag" @click="saveDialog()" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {getQuestionnaireInfo,saveQuestionnaire} from '@/api/questionManage/question';
import questionInfo from './info';
export default {
  name: "questionnaireInfo",
  components: {questionInfo},
  mixins: [dictionary],
  props:{
    value: {
      type: Object,
      data: Object,
      required: true
    },
  },
  data() {
    return {
      type: this.value.type,
      initForm : this.initFormFun(),
      questionColumns: this.creatHeaderPartRoleColumns(),
      partList: [],
      isSelectColumn: false,
      visibleQuestion:false,
      searchData: {},
      returnID: '',
      saveData: {
        editType: '',
        xxList: []
      },
      djzt: null,
      isShow: true,
      loadingFlag:false,
    };
  },
  created() {
    this.init(this.value.data)
  },
  methods: {
    init(data) {
      if(this.type==='edit'){
        this.initForm.forEach(x => {
          x.initialValue = data[x.fieldName];
        });
        var initdata = {
          wxwjid: data.wxwjid,
        }
        this.saveData.wxwjid = data.wxwjid
        getQuestionnaireInfo(initdata).then(response => {
          console.info(111)
          console.info(response)
          var list = response.data.records
          if(response.resultCode === 200){
            this.$nextTick(()=>{
              this.$refs.tableQuestion.EXECUTE_INSERT(list)
            })
          }
        })
      }
    },
    initFormFun(){
      return [
        {
          type: 'INPUT',
          label: '问卷代码',
          fieldName: 'wxwjdm',
          disabled: this.value.type === 'edit',
          rules: [{
            required: true,
            message: '必填',
            trigger: 'change'
          }]
        },
        {
          type: 'INPUT',
          label: '问卷名称',
          fieldName: 'wxwjmc',
          rules: [{
            required: true,
            message: '必填',
            trigger: 'change'
          }]
        },
        {
          type: 'SELECT',
          label: '启用标记',
          fieldName: 'wxwjbz',
          placeholder: '请选择',
          clearable: false,
          initialValue: 10031001,
          itemList: this.createDictList('1003')
        },
        {
          type: 'SELECT',
          label: '问卷类型',
          fieldName: 'wxwjlx',
          placeholder: '请选择',
          clearable: false,
          initialValue: 50191001,
          itemList: this.createDictList('5019'),
          change: val => {
            this.lxChange(val);
          }
        }
      ]
    },
    creatHeaderPartRoleColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.deletePart(props.row,props.$index)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.$index + 1}</span>;
          }
        },
        {
          title: '问题代码',
          dataIndex: 'wxwtdm',
          filterType: 'input',
        },
        {
          title: '问题内容',
          dataIndex: 'wxwtnr',
          filterType: 'input'
        },
        {
          title: '问题类型',
          dataIndex: 'wxwtlx',
          sorter: true,
          render: props => { return <span>{this.createDictText(props.row.wxwtlx, '5018')}</span>; }
        },
        {
          title: '启用标记',
          dataIndex: 'wxsfqy',
          sorter: true,
          render: props => { return <span>{this.createDictText(props.row.wxsfqy, '1003')}</span>; }
        }
      ];
    },
    changeHandle(val){
      this.saveData=val
    },
    //保存
    async saveDialog() {
      let [err, data] = await this.$refs.from.GET_FORM_DATA();
      if(err) return;
      console.info(33)
      console.info(this.saveData)

      if(this.saveData.xxList.length<1) {
        this.$notify.error({title: '提示信息',message:'问卷问题需大于0!'});
        return
      }


      if (this.saveData) {
        this.saveData.editType = this.type
        this.saveData.wxwjdm = data.wxwjdm
        this.saveData.wxwjmc = data.wxwjmc
        this.saveData.wxwjlx = data.wxwjlx
        this.saveData.wxwjbz = data.wxwjbz
        this.loadingFlag = true;
        console.info(444)
        console.info(this.saveData)
        let res = await saveQuestionnaire(this.saveData)
        console.info(res)
        if (res.data.status === 'success') {
          this.$notify.success({title: '提示信息',message:'保存成功!'});
          this.loadingFlag = false;
          this.closeDialog('refresh')
        } else {
          this.$notify.error({title: '提示信息',message:res.data.message||'保存失败!'});
          this.loadingFlag = false;
        }
      }
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', type);
    },
    syncPjFun(details){
      this.saveData.xxList = details
    },
    addPositionFun() {
      var flag = false;
      this.initForm.forEach(x => {
        if (x.fieldName === 'wxwjlx') {
          if (x.initialValue === '' || x.initialValue === null || x.initialValue === undefined) {
            flag = true;
          }
          this.searchData.wxwjsy = x.initialValue
        }
      });
      if(flag){
        this.$notify.error({ title: '提示信息', message: '请先选择问卷类型!' });
        return false;
      } else {
        this.visibleQuestion = true
        return true;
      }
    },
    deletePart(row,index) {
      this.$confirm('确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        this.$refs.tableQuestion.EXECUTE_DELETE([row])
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      })
      .catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    selectedFun(val) {
      val.forEach(x => {
        x.flag = true
        this.saveData.xxList.forEach(item => {
          if (item.wxwtdm === x.wxwtdm) {
            x.flag = false
          }
        })
      })
      val.forEach(x => {
        if (x.flag === true) {
          this.$refs.tableQuestion.EXECUTE_INSERT(x)
        }
      })
      this.visibleQuestion = false;
    },
    lxChange(val) {
      this.$refs.tableQuestion.SET_TABLE_DATA([])
    }
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
